/* * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hookedonplay.decoviewlib.util;

import ohos.agp.components.AttrSet;

public class AttrUtils {
    // Get int value from Attributes
    public static Integer getIntFromAttr(AttrSet attrs, String name, Integer defaultValue) {
        Integer value = defaultValue;
        try {
            if (attrs.getAttr(name).isPresent()) {
                value = attrs.getAttr(name).get().getIntegerValue();
            }
        } catch (NullPointerException e) {
            e.getMessage();
        }
        return value;
    }

    // Get float value from Attributes
    public static float getDimensionFromAttr(AttrSet attrs, String name, int defaultValue) {
        float value = defaultValue;
        try {
            if (attrs.getAttr(name).isPresent()) {
                value = attrs.getAttr(name).get().getDimensionValue();
            }
        } catch (NullPointerException e) {
            e.getMessage();
        }
        return value;
    }

    // Get int horizontal gravity value from Attributes
    public static int getHGravFromAttr(AttrSet attrs, String name, int defaultValue) {
        int value = defaultValue;
        try {
            if (attrs.getAttr(name).isPresent() && attrs.getAttr(name).get() != null) {
                String v = attrs.getAttr(name).get().getStringValue();
                switch (v) {
                    case "Left":
                        value = 0;
                        break;
                    case "Center":
                        value = 1;
                        break;
                    case "Right":
                        value = 2;
                        break;
                    case "Fill":
                        value = 3;
                        break;
                }
            }
        } catch (NullPointerException e) {
            e.getMessage();
        }
        return value;
    }

    // Get int vertical gravity value from Attributes
    public static int getVGravFromAttr(AttrSet attrs, String name, int defaultValue) {
        int value = defaultValue;
        try {
            if (attrs.getAttr(name).isPresent() && attrs.getAttr(name).get() != null) {
                String v = attrs.getAttr(name).get().getStringValue();
                switch (v) {
                    case "Top":
                        value = 0;
                        break;
                    case "Center":
                        value = 1;
                        break;
                    case "Bottom":
                        value = 2;
                        break;
                    case "Fill":
                        value = 3;
                        break;
                }
            }
        } catch (NullPointerException e) {
            e.getMessage();
        }
        return value;
    }
}